Skip to content

BUG: Categorical(Index) passed as categories #17888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 18, 2017

Conversation

jorisvandenbossche
Copy link
Member

@jorisvandenbossche jorisvandenbossche commented Oct 16, 2017

@TomAugspurger I am not sure we had a discussion about that before, but to me it does not make many sense to have a CategoricalIndex as the categories of a Categorical/CategoricalIndex, so I think we should convert passed categories (which is what this PR is doing).
Alternative to fix #17884 is to fix the repr code to deal with Categorical categories.

@@ -316,7 +315,10 @@ def _validate_categories(categories, fastpath=False):
from pandas import Index

if not isinstance(categories, ABCIndexClass):
categories = Index(categories)
categories = Index(categories, tupleize_cols=False)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be an elif and instead should use is_categorical

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the point of the ABC ones to avoid imports from itself ? (as is_categorical uses CategoricalDtype)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback Can you answer this one? No problem to change it if you think it is better, but it seems to me using isinstance(categories, ABCCategoricalIndex) is consistent with the use of isinstance(categories, ABCIndexClass) just above (also in readability of the code)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, yeah I guess you have to do this. but you can also check isinstance(categories, (ABCCateoricalIndex, ABCCategorical)) is more generic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to use isinstance(categories, (ABCCateoricalIndex, ABCCategorical)) first and then only to check for Index

@TomAugspurger TomAugspurger added this to the 0.21.0 milestone Oct 16, 2017
@TomAugspurger
Copy link
Contributor

Thanks Joris, agreed with the changes here. Looks like this conflicts with your fastpath PR.

@codecov
Copy link

codecov bot commented Oct 16, 2017

Codecov Report

Merging #17888 into master will decrease coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #17888      +/-   ##
==========================================
- Coverage   91.23%   91.22%   -0.02%     
==========================================
  Files         163      163              
  Lines       50105    50103       -2     
==========================================
- Hits        45714    45704      -10     
- Misses       4391     4399       +8
Flag Coverage Δ
#multiple 89.03% <100%> (ø) ⬆️
#single 40.31% <83.33%> (-0.07%) ⬇️
Impacted Files Coverage Δ
pandas/core/dtypes/dtypes.py 95.15% <100%> (+0.01%) ⬆️
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/core/frame.py 97.75% <0%> (-0.11%) ⬇️
pandas/core/indexes/datetimes.py 95.5% <0%> (+0.09%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9092445...ab23d41. Read the comment docs.

@codecov
Copy link

codecov bot commented Oct 16, 2017

Codecov Report

Merging #17888 into master will decrease coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #17888      +/-   ##
==========================================
- Coverage   91.23%   91.22%   -0.02%     
==========================================
  Files         163      163              
  Lines       50105    50105              
==========================================
- Hits        45714    45706       -8     
- Misses       4391     4399       +8
Flag Coverage Δ
#multiple 89.03% <100%> (ø) ⬆️
#single 40.31% <83.33%> (-0.07%) ⬇️
Impacted Files Coverage Δ
pandas/core/dtypes/dtypes.py 95.14% <100%> (ø) ⬆️
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/core/frame.py 97.75% <0%> (-0.1%) ⬇️
pandas/core/indexes/datetimes.py 95.5% <0%> (+0.09%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9092445...1c56abe. Read the comment docs.


result = pd.Categorical(
['a', 'b'], categories=pd.CategoricalIndex(['a', 'b', 'c']))
tm.assert_categorical_equal(result, expected)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Categorical and CategoricalIndex have been imported, so you can remove the pd. here, if you feel that's cleaner.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, thanks! (updated)


result = pd.Categorical.from_codes(
[0, 1], categories=pd.CategoricalIndex(['a', 'b', 'c']))
tm.assert_categorical_equal(result, expected)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same regarding pd.

@jreback jreback added the Categorical Categorical Data Type label Oct 16, 2017
@@ -1023,6 +1023,7 @@ Categorical
- Bug in the categorical constructor with empty values and categories causing the ``.categories`` to be an empty ``Float64Index`` rather than an empty ``Index`` with object dtype (:issue:`17248`)
- Bug in categorical operations with :ref:`Series.cat <categorical.cat>` not preserving the original Series' name (:issue:`17509`)
- Bug in :func:`DataFrame.merge` failing for categorical columns with boolean/int data types (:issue:`17187`)
- Bug in constructing a ``Categorical``/``CategoricalDtype`` when the specified ``categories`` where of categorical type (:issue:`17884`).
Copy link
Contributor

@jreback jreback Oct 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this to the CDT issue, IOW the sub-section (as its actually not an independent bug) and better there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug also exists for Categorical and CateforicalIndex and was already present in current released 0.20.3. So not directly related to the CDT rework (although the bug is present in CDT as well). But not that important, can also move if you want.

@jorisvandenbossche
Copy link
Member Author

One disadvantage of the my changes is that is somehow relaxes the "unique" requirement for the passed categories. Because now you can pass a non-unique Categorical, and we don't raise anymore but just take its unique categories.
I think it seems obvious that this is what the user want, but still, I am not sure relaxing the requirement is a good idea (eg in from_codes it means the passed codes don't match exactly the passed categories). But actually I can easily check this by moving the check for Categorical values after the check for unique / nulls.

@jreback
Copy link
Contributor

jreback commented Oct 17, 2017

@jorisvandenbossche ping when updated.

@jorisvandenbossche
Copy link
Member Author

As far as I am concerned, this is updated. Is there a comment I overlooked?

@@ -326,6 +326,9 @@ def _validate_categories(categories, fastpath=False):
if not categories.is_unique:
raise ValueError('Categorical categories must be unique')

if isinstance(categories, ABCCategoricalIndex):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here you an also accept ABCCategorical

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a Categorical has already been converted to CategoricalIndex

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh ok

@jreback jreback merged commit a0a0f5a into pandas-dev:master Oct 18, 2017
@jorisvandenbossche jorisvandenbossche deleted the cat-constructor branch October 18, 2017 07:53
yeemey pushed a commit to yeemey/pandas that referenced this pull request Oct 20, 2017
alanbato pushed a commit to alanbato/pandas that referenced this pull request Nov 10, 2017
No-Stream pushed a commit to No-Stream/pandas that referenced this pull request Nov 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Categorical.from_codes does not allow categories to be a CategoricalIndex
4 participants